package org.apache.mina.protocol.dns.nameserver;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

import org.apache.mina.protocol.dns.DnsResolver;

/**
 * Tries to get DNS name servers addresses from system properties 
 * 
 * Used to configure a {@link DnsResolver}.
 * 
 * This class is package private because should only be used by
 * {@link NameServerConfigurationFactory}.
 *
 * @author The Apache MINA Project (dev@mina.apache.org)
 * @version $Rev: 83 $, $Date: 2008-02-22 15:37:26 +0000 (Fri, 22 Feb 2008) $
 */
class PropertiesConfiguration implements NameServerConfiguration {

    private List<SocketAddress> servers = new ArrayList<SocketAddress>();

    private List<String> domains = new ArrayList<String>();

    public PropertiesConfiguration() {

        String prop = System.getProperty("dns.server");
        if (prop != null) {
            for (String addr : prop.split(",")) {
                servers.add(new InetSocketAddress(addr, DNS_PORT));
            }
        }

        String searchDomains = System.getProperty("dns.search");
        if (searchDomains != null) {
            for (String d : searchDomains.split(",")) {
                domains.add(d);
            }
        }
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.nameserver.NameServerConfiguration#getNameServers()
     */
    public List<SocketAddress> getNameServers() {
        return servers;
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.nameserver.NameServerConfiguration#getDomains()
     */
    public List<String> getDomains() {
        return domains;
    }

    /* (non-Javadoc)
     * @see org.apache.mina.protocol.dns.nameserver.NameServerConfiguration#isOk()
     */
    public boolean isOk() {
        return servers.size() > 0;
    }

}
